package main

import (
	"fmt"
	"encoding/json"
)

type Student struct {
	Name    string
	Age        int
	Guake    bool
	Classes    []string
	Price    float32
}

func (s * Student)ShowStu() {
	fmt.Println("show Student :")
	fmt.Println("\tName\t:", s.Name)
	fmt.Println("\tAge\t:", s.Age)
	fmt.Println("\tGuake\t:", s.Guake)
	fmt.Println("\tPrice\t:", s.Price)
	fmt.Printf("\tClasses\t: ")
	for _, a := range s.Classes {
		fmt.Printf("%s ", a)
	}
	fmt.Println("")
}

func main() {
	st := &Student {
		"Xiao Ming",
		16,
		true,
		[]string{"Math", "English", "Chinese"},
		9.99,
	}
	fmt.Println("before JSON encoding :")
	st.ShowStu()

	b, err := json.Marshal(st)
	if err != nil {
		fmt.Println("encoding faild")
	} else {
		fmt.Println("encoded data : ")
		fmt.Println(b)
		fmt.Println(string(b))
	}

	jsonObj := string(b)

	var interfaceTmep Student

	json.Unmarshal([]byte(jsonObj), &interfaceTmep)

	fmt.Println(interfaceTmep)

	//ch := make(chan string, 1)
	//go func(c chan string, str string){
	//	c <- str
	//}(ch, string(b))
	//strData := <-ch
	//fmt.Println("--------------------------------")
	//stb := &Student{}
	//stb.ShowStu()
	//err = json.Unmarshal([]byte(strData), &stb)
	//if err != nil {
	//	fmt.Println("Unmarshal faild")
	//} else {
	//	fmt.Println("Unmarshal success")
	//	stb.ShowStu()
	//}
}